package com.nvc.homeplan;



import com.nvc.homeplan.R.drawable;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.AnimationUtils;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import com.nvc.homeplan.data.vo.*;

public class PlanDetailPage  extends Activity 
{
    private PlanVO planVO;
    private static final String CLASSTAG = PlanDetailPage.class.getSimpleName();
    private HomePlanApplication application;

    //private ProgressDialog progressDialog;
    ////private final ImageDownloader imageDownloader = new ImageDownloader();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.v(Constants.LOGTAG, " " + PlanDetailPage.CLASSTAG + " onCreate");
        this.setContentView(R.layout.plan_detail_page);
        
        // get the current review from the Application (global state placed there)
        application = (HomePlanApplication) getApplication();
        
        WebView webView = (WebView) findViewById(R.id.webview);
        WebSettings webSettings = webView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webView.addJavascriptInterface(new JavaScriptInterface(this), "Android");
        String url = Constants.baseDataUrl + "mobileDetail.cfm?switch=" + application.cfmswitch + "&number=" + application.getPlanID();
        webView.loadUrl( url );
    }   
    
    @Override
    protected void onResume() {
        super.onResume();
        
        //loadPlan();
    }    
    
    private void loadPlan() 
    {
        //this.progressDialog = ProgressDialog.show(this, " Working...", " Retrieving Data", true, false);
        new Thread() {
            @Override
            public void run() {
            	try {
					Thread.sleep(1);
					com.nvc.homeplan.data.Plan plan = new com.nvc.homeplan.data.Plan();
					planVO = plan.getSinglePlan(application.getPlanID());
				} 
            	catch (InterruptedException e) 
            	{
					e.printStackTrace();
				}
                handler.sendEmptyMessage(0);
            }
        }.start(); 
    }
    
    private final Handler handler = new Handler() {
        @Override
        public void handleMessage(final Message msg) {
        	/*
            Log.v(Constants.LOGTAG, " " + PlanDetailPage.CLASSTAG + " worker thread done, setup PlanListAdapter");
        	//progressDialog.dismiss();
            if (planVO != null) 
            {


            	
                ImageView img = (ImageView) findViewById(R.id.plan_image);
                imageDownloader.download(planVO.imageURL, (ImageView) img);
                
                StringBuilder sb = new StringBuilder();
                sb.append("House Plan: " + application.getPlanID() + "\n");
                sb.append("Total Living Area: " + planVO.finishedsf + "\n");
                sb.append("Bedroom: " + planVO.noOfBed+ "\n");
                sb.append("Bath: " + planVO.noOfBath+ "\n");
                
                TextView planSpecification = (TextView) findViewById(R.id.plan_specification);
                planSpecification.setText(sb.toString());
            	
            }
            */
        }
    };   
    
    
    public class JavaScriptInterface {
        Context mContext;

        /** Instantiate the interface and set the context */
        JavaScriptInterface(Context c) {
            mContext = c;
        }

        /** Show a toast from the web page */
        public void showImage(String imageURL) 
        {
        	HomePlanApplication application = (HomePlanApplication) getApplication();
        	application.imageURL = imageURL;
        	
            Intent intent = new Intent(Constants.INTENT_ACTION_IMAGE_VIEW_PAGE);
            // startFrom page is not stored in application, for example purposes it's a simple "extra"
            //intent.putExtra(Constants.STARTFROM_EXTRA, getIntent().getIntExtra(Constants.STARTFROM_EXTRA, 1));
            startActivity(intent);
        	
            //Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
        }
    }    
}
